import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbactivityinformation } from '../../../atoms/db';
import uuid from '../../../atoms/uuid';

const logger = anylogger('pages/api/activity/addlaunch.api');

export type Result = {
	ok: boolean;
	message: string;
};


export type Message = ITbactivityinformation;

/**
 * 发起活动-新增
 */
const handler = an48<Result>();

handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const { theme, ...body } = req.body as Message;
		//必填校验
		if (!theme) {
			res.status(200).json({ ok: false, message: '活动主题不能为空' });
			return;
		}
		//验证数据是否存在
		const d = await tbactivityinformation().first('*').where({
			theme
		});
		if (d && d.theme) {
			res.status(200).json({ ok: false, message: '活动主题不能重复' });
			return;
		}
		//新增数据
		await tbactivityinformation().insert({
			aid: uuid(),
			theme,
			...body
		});
		res.status(200).json({ ok: true, message: '' });
	} catch (error) {
		logger.error(error);
		logger.trace(error);
		res.status(200).json({ ok: false, message: (error as Error).message });
	}
});

export const config = {} as PageConfig;

export default handler;
